package org.apache.dubbo.common.context;

import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.AbstractConfig;
import org.apache.dubbo.common.config.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * This class receives an {@link AbstractConfig} and exposes its attributes through {@link Configuration}
 */
public class ConfigConfigurationAdapter implements Configuration {

    private final Map<String,String> metaData;

    public ConfigConfigurationAdapter(AbstractConfig config) {
        Map<String,String> configMetaData = config.getMetaData();
        metaData = new HashMap<>(configMetaData.size());
        for (Map.Entry<String,String> entry : configMetaData.entrySet()) {
            String prefix = config.getPrefix().endsWith(".") ? config.getPrefix() : config.getPrefix() + ".";
            String id = StringUtils.isEmpty(config.getId()) ? "" : config.getId() + ".";
            metaData.put(prefix + id + entry.getKey(),entry.getValue());
        }
    }

    @Override
    public Object getInternalProperty(String key) {
        return metaData.get(key);
    }
}
